﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using DG.Tweening;
using UnityEngine.UI;

public class DragPancke : MonoBehaviour,IDragHandler,IEndDragHandler,IBeginDragHandler {
	public Text PanScore;
	private bool EndDrag;
	Vector3 storepos;
	Vector3 storeScale;
	Text Score;


	public void OnBeginDrag(PointerEventData eventData){
		if (EndDrag == false) {
			storepos = transform.position;
			storeScale = transform.localScale;
		}
	}

	public void OnDrag(PointerEventData eventData){
		
		if (EndDrag == false) {
			transform.DOKill ();
			transform.position = Input.mousePosition;
			transform.DOScale (1f, 0.1f);
		}

	}
	public void OnEndDrag(PointerEventData eventData){
		
		if (EndDrag == false) {
			EndDrag = true;
			if (transform.position.y > Camera.main.GetComponent<Global> ().Arrow.transform.position.y) {
				transform.DOKill ();
				transform.DOScale (0.56f, 0.1f);
				transform.DOMove (storepos, 0.1f);
				EndDrag = false;

			} 

			else {
				Global.PanCakesNumber += 1;

				PanScore.text = Global.PanCakesNumber.ToString ();
					Vector3 pankPos = Camera.main.ScreenToWorldPoint (transform.position);
				if (Camera.main.GetComponent<Global> ().MyPancakes.Count > 0) {
					if (pankPos.y < Camera.main.GetComponent<Global> ().MyPancakes [Camera.main.GetComponent<Global> ().MyPancakes.Count - 1].transform.position.y) {
						pankPos.y = Camera.main.GetComponent<Global> ().MyPancakes [Camera.main.GetComponent<Global> ().MyPancakes.Count - 1].transform.position.y + 0.5f;
					}
				}
					GameObject ojb =	Instantiate (Camera.main.GetComponent<Global> ().PhysicPancake, new Vector3 (pankPos.x, pankPos.y, 0f), Quaternion.Euler (0f, 0f, 0f));
				ojb.GetComponent<SpriteRenderer> ().sortingOrder = Global.PanCakesNumber;
				Camera.main.GetComponent<Global> ().MyPancakes.Add (ojb);
					transform.DOKill ();
					transform.DOScale (0.56f, 0f);

				transform.GetChild (0).GetComponent<Image> ().DOKill ();
				transform.GetChild (0).GetComponent<Image> ().DOFade (0.8f, 0f);
				transform.GetChild (0).GetComponent<Image> ().DOFade (0f, 1.5f);

					transform.position = storepos;
					EndDrag = false;
				// Check AndGrowCamera
				Vector3 comparepos = Camera.main.ScreenToWorldPoint (transform.position);
				if (Camera.main.GetComponent<Global> ().MyPancakes.Count > 1) {
					if (comparepos.y - Camera.main.GetComponent<Global> ().MyPancakes [Camera.main.GetComponent<Global> ().MyPancakes.Count - 2].transform.position.y < 3f) {
						Camera.main.GetComponent<Global> ().GrowCamera ();
					}
				}
			}

		}
	}

	void Update () {
		
	}

    public void TouchWatchAds()
    {
        string videoAdid = VideData.Instance.GetVide();
        TTSDK.TTRewardedVideoAd r = AdMgr.Instance.ShowVideoAdNew(videoAdid);
        //创建激励视频关闭回调
        r.OnClose += (bol, val) =>
        {
            Debug.Log("关闭广告 ，是否已经看完" + bol);
            //看完广告获得提示
            if (bol)
            {
                Global.PanCakesNumber += 1;

                PanScore.text = Global.PanCakesNumber.ToString();
                MaidianNew.MaidianBackCallback(" ");
                Debug.Log("广告看完");
            }
            else
            {
                Debug.Log("广告未看完");
            }
        };

        //创建报错回调  
        r.OnError += (code, errorMessage) =>
        {

            Debug.Log("code:" + code + "错误信息：" + errorMessage);
        };

        //展示广告（新版本必须调用，不然无法展示出来广告）
        r.Show();

    }
}
